package com.small.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.small.common.ServerResponse;
import com.small.dao.CategoryMapper;
import com.small.pojo.Category;
import com.small.service.ICategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Set;


/**
 * @program: small
 * @description:
 * @author: Mr.Xie
 * @create: 2019-04-24 19:24
 */
@Service("iCategoryServiceImp")
@Slf4j
public class CategoryServiceImp implements ICategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加商品分类
    *@Param:
    *@return:
    */
    public ServerResponse addCategory(String categoryName,Integer parentId){
        if(parentId==null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("添加商品分类信息错误");
        }
        Category category=new Category();
        category.setName(categoryName);
        category.setParentId(parentId);
        category.setStatus(true);//设置这个分类是可用的

        int rowCount =categoryMapper.insert(category);
        if(rowCount>0){
            return ServerResponse.createBySuccessMessage("添加分类成功");
        }
        return ServerResponse.createByErrorMessage("添加分类失败");
    }
    /**
     * 修改品类名称
    */
    public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
        if(categoryId==null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("更新商品分类信息错误");
        }
        Category category=new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if(rowCount>0){
            return ServerResponse.createBySuccessMessage("更新品类名称成功");
        }
        return ServerResponse.createByErrorMessage("更新品类名称失败");
    }
    //查看子分类
    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer parentId){
        List<Category> list=categoryMapper.selectCategoryChildrenByParentId(parentId);
        if(CollectionUtils.isEmpty(list)){
            log.info("未找到当前分类的子分类");
        }
        return ServerResponse.createBySuccess(list);
    }
    /**
     * 递归查询本节点id和子节点id
    */
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
        Set<Category> categorySet= Sets.newHashSet();
        findChildrenCategory(categorySet, categoryId);

        List<Integer> categoryIdList= Lists.newArrayList();
        if(categoryId!=null){
            for (Category categoryItem:categorySet) {
                categoryIdList.add(categoryItem.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryIdList);
    }
    //递归算法，算出子节点
    private Set<Category> findChildrenCategory(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if(category!=null){
            categorySet.add(category);
        }
        //查找子节点，递归算法一定要有一个退出条件
        List<Category> categorylist = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for (Category categoryItem : categorylist) {
            findChildrenCategory(categorySet,categoryItem.getId());
        }
        return categorySet;
    }
}
